package br.com.ffm.praticas.facade;

import java.sql.SQLException;
import java.util.List;

import br.com.ffm.praticas.bean.SingleCostumer;
import br.com.ffm.praticas.dao.SingleCostumerDAO;

public class SingleCostumerFACADE {
	
	private static SingleCostumerFACADE singleCostumerFACADE;
	private static SingleCostumerDAO singleCostumerDAO;
	
	/**
	 * Constructor of SingleCostumerFACADE
	 */
	private SingleCostumerFACADE(){
		singleCostumerDAO = new SingleCostumerDAO();
	}
	
	/**
	 * Method that returns an unique instance of SingleCostumerFACADE
	 * @return An instance of SingleCostumerFACADE
	 */
	public static SingleCostumerFACADE getInstance(){
		if(singleCostumerFACADE == null){
			singleCostumerFACADE = new SingleCostumerFACADE();
		}
		return singleCostumerFACADE;
	}
	
	/**
	 * Method that checks if a SingleCostumer exist
	 * @param singleCostumer The SingleCostumer to be checked
	 * @return If exist returns true
	 * @throws SQLException
	 */
	private boolean alreadyExist(SingleCostumer singleCostumer) throws SQLException{
		SingleCostumer sc = singleCostumerDAO.getSingleCostumer(singleCostumer.getId());
		if(sc != null){
			return true;
		}
		return false;
	}
	
	/**
	 * Method that insert a SingleCostumer
	 * @param singleCostumer The SingleCostumer to be inserted
	 * @return If insert returns true
	 * @throws SQLException
	 */
	public boolean insert(SingleCostumer singleCostumer) throws SQLException{
		if(singleCostumer != null && !alreadyExist(singleCostumer)){
			singleCostumerDAO.insert(singleCostumer);
			return true;
		}
		return false;
	}
	
	/**
	 * Method that delete a SingleCostumer
	 * @param singleCostumer The SingleCostumer to be deleted
	 * @return If delete returns true
	 * @throws SQLException
	 */
	public boolean delete(SingleCostumer singleCostumer) throws SQLException{
		if(singleCostumer != null && alreadyExist(singleCostumer)){
			singleCostumerDAO.delete(singleCostumer);
			return true;
		}
		return false;
	}
	
	/**
	 * Method that find and return a SingleCostumer
	 * @param id The id of SingleCostumer to be found
	 * @return If found returns the SingleCostumer
	 * @throws SQLException
	 */
	public SingleCostumer getSingleCostumer(int id) throws SQLException{
		return singleCostumerDAO.getSingleCostumer(id);
	}
	
	/**
	 * Method that returns all SingleCostumers
	 * @return All inserted SingleCostumers
	 * @throws SQLException
	 */
	public List<SingleCostumer> getSingleCostumers() throws SQLException{
		return singleCostumerDAO.getSingleCostumers();
	}
	
	/**
	 * Method that update a SingleCostumer
	 * @param singleCostumer The SingleCostumer to be updated
	 * @return If update returns true
	 * @throws SQLException
	 */
	public boolean update(SingleCostumer singleCostumer) throws SQLException{
		if(singleCostumer != null && alreadyExist(singleCostumer)){
			singleCostumerDAO.update(singleCostumer);
			return true;
		}
		return false;
	}
	
	/**
	 * Method that returns the number of SingleCostumers
	 * @return The number of SingleCostumers
	 * @throws SQLException 
	 */
	public int count() throws SQLException{
		return singleCostumerDAO.getSingleCostumers().size();
	}

}
